const axios = require("axios").default.create({
  headers: {
    "User-Agent": "MMM / 0.0.0"
  }
})

/**
 * 
 * @param {String} title 
 * @param {String} content 
 */
function logToRemote(title, content) {
  console.log("title", title)
  const webhooklink = 'https://hook.worktile.com/incoming/71e070230fca40fa8dc2bdaea507b003';
  axios.post(webhooklink, {
    text: `:: ${title}
    \n ${content}
    \n
    \n---`
  }, {
    headers: {
      "Content-Type": "application/json"
    }
  }).then(axresp => {
    console.log("webhookok");
  }).catch(axerr => {
    console.log("axerr", axerr);
  })
}

const child_process = require("child_process");
const path = require("path");

function redo() {
  let cp = child_process.exec("node " + path.join(__dirname, "./index.js"));
  cp.stdout.once("data", (chunk) => {
    console.log(chunk);
    logToRemote("start running ..",
      `chunk is ${chunk}`);
  })
  cp.on("exit", (code, sig) => {
    console.log("code", code);
    console.log("sig", sig);
    logToRemote("exit code signal", `code = ${code} signal = ${sig}
    \n try redo...`);
    setTimeout(() => redo(), 1000);
  })
}


redo();

